using System;
using System.Collections.Generic;
using System.Text;
using Xeml.Document.Contracts;
using Mpi.Common.Collections;
using Xeml.Manager;
using System.Diagnostics;
using Xeml.Document.Manager;
using System.ComponentModel;
using Xeml.Document.CoreObjects;

namespace Xeml.Document
{
    /// <summary>
    /// Describes and requests an instance of an ontology handler.
    /// </summary>
    public class OntologyHandlerResource : INotifyPropertyChanged
    {
        #region Fields
        private string _nameSpace;
        private string _location;
        private string _uri;
        private IOntologyHandler _handler;
        private ComponentLoadState _compState = ComponentLoadState.Untouched;
        #endregion

        #region Properties
        /// <summary>
        /// The uri of the handler which will be loaded by the framework.
        /// </summary>
        public string Uri
        {
            get { return _uri; }

        }
        /// <summary>
        /// The referenced validator component instance.
        /// </summary>
        public IOntologyHandler Handler
        {
            get
            {
                return this._handler;
            }
        }

        /// <summary>
        /// The current state of the referenced component.
        /// </summary>
        public ComponentLoadState ComponentState
        {
            get { return _compState; }
            set { _compState = value; }
        }

        /// <summary>
        /// The name space for the parameter.
        /// </summary>
        public string NameSpaceAlias
        {
            get
            {
                return this._nameSpace;
            }
            set
            {
                string oldVal = this._nameSpace;
                if (oldVal != value)
                {
                    this._nameSpace = value;
                    OnPropertyChanged("NameSpaceAlias");
                }
            }
        }

        private int calCount = 0;
        /// <summary>
        /// The location for the ontology instance.
        /// </summary>
        public string Location
        {
            get
            {
                return this._location;
            }
            set
            {
                calCount++;
                Debug.WriteLine("OntologyHandlerResource: call count Location=" + calCount);
                string oldVal = this._location;
                string newVale = OnlineCacheManager.Instance.ResolveUri(value);
                if (oldVal != newVale)
                {
                    this._location = newVale;
                    if (this._handler != null)
                    {
                        this._handler.InstanceLocation = _location;
                        //this._handler.InstanceLocation = value;
                    }
                }
            }
        }
        #endregion

        public delegate void MessageEmittedEventHandler(object sender, MessageEventArgs e);
        public event MessageEmittedEventHandler MessageEmitted;
 
        public void StatusUpdate(string message)
        {
            if (MessageEmitted != null)
            {
                MessageEmitted(this, new MessageEventArgs(message, EventLogEntryType.Information));
            }
        } 

        public OntologyHandlerResource(string uri)
        {
            _uri = uri;
        }

        #region Methods
        public void LoadOntology()
        {
            if (this.ComponentState == ComponentLoadState.Loaded)
            {
                if (this._handler.OntologyLoadState != OntologyLoadState.Loaded)
                {
                    StatusUpdate("Processing ontology.");
                    this._handler.Load(_nameSpace);
                }
            }
        }

        /// <summary>
        /// Loads the referenced component.
        /// </summary>
        public void LoadComponent()
        {
            if (_compState != ComponentLoadState.Loaded)
            {
                try
                {
                    StatusUpdate("Loading ontology.");
                    this._handler = this._location != null && this._location != "" ?
                        OntologyManager.Instance.CreateHandler(this._uri, this._location) :
                        OntologyManager.Instance.CreateHandler(this._uri);
                    this._handler.InstanceLocation = this._location;
                    this.LoadOntology();
                    //this.handler.NameSpace = this.NameSpaceAlias;
                    this._compState = ComponentLoadState.Loaded;
                }
                catch (XemlComponentLoadFailedException)
                {
                    this._compState = ComponentLoadState.Failed;
                }
            }
        }
        #endregion

        private HandlerResourceCollection _collection;
        public HandlerResourceCollection Collection
        {
            get
            {
                return this._collection;
            }
            set
            {
                if (value != _collection)
                {
                    this._collection = value;
                }

            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        #endregion
    }
}
